package com.example.bbook.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bbook.entity.SpeciesEntity;
import com.example.bbook.mapper.SpeciesMapper;
import com.example.bbook.services.CRUDServiceInterface;
import com.example.bbook.util.HashMapTool;
import com.example.bbook.util.PageTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class SpeciesServiceImpl implements CRUDServiceInterface<SpeciesEntity> {
    @Autowired
    private BaseMapper<SpeciesEntity> baseMapper;

    @Autowired
    private SpeciesMapper speciesMapper;

    @Override
    public HashMap<String, Object> findAll(PageTool pageConfig, SpeciesEntity data) {
        HashMapTool hashMapTool = new HashMapTool();
        Integer page = 1, pageSize = 10;
        if (pageConfig.normal()) {
            page = pageConfig.getPage();
            pageSize = pageConfig.getPageSize();
        }
        IPage<SpeciesEntity> userPage = new Page<>(page, pageSize);
        QueryWrapper<SpeciesEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("id");
        if (data.getDictType() != null) {
            userQueryWrapper.eq("dict_type", data.getDictType());
        }

        if (data.getTitle() != null) {
            userQueryWrapper.like("title", data.getTitle());
        }

        if (data.getName() != null) {
            userQueryWrapper.like("name", data.getName());
        }

        if (data.getParentId() != null) {
            userQueryWrapper.eq("parent_id", data.getParentId());
        }

        ArrayList<HashMap<String, Object>> speciesTypeArr = new ArrayList<>();

        QueryWrapper<SpeciesEntity> speciesEntityQueryWrapper = new QueryWrapper<>();
        speciesEntityQueryWrapper.eq("dict_type", "goods");
        speciesEntityQueryWrapper.in("parent_id", 0);
        List<SpeciesEntity> speciesEntityList = speciesMapper.selectList(speciesEntityQueryWrapper);
        speciesTypeArr.add(hashMapTool.getListKeyValue("none", 0, "父类型"));
        speciesEntityList.forEach(item -> {
            speciesTypeArr.add(hashMapTool.getListKeyValue(item.getName(), item.getId(), item.getTitle()));
        });
        userPage = baseMapper.selectPage(userPage, userQueryWrapper);
        List<SpeciesEntity> speciesEntities = userPage.getRecords();

        speciesEntities.forEach(item -> {
            if (item.getParentId() == 0) {
                SpeciesEntity parent = new SpeciesEntity();
                parent.setTitle("父类型");
                parent.setDictType("other");
                parent.setName("parent");
                item.setParent(parent);
            } else {
                item.setParent(speciesMapper.selectById(item.getParentId()));
            }
        });

        SpeciesEntity speciesEntity = new SpeciesEntity();

        speciesEntity.push("parentId", speciesTypeArr, "父类型", new int[]{1, 1, 1, 1}, new String[]{"parent", "title"});


        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("recode", speciesEntities);
        resMap.put("columns", speciesEntity.thisColumns());
        resMap.put("total", baseMapper.selectCount(userQueryWrapper));
        return resMap;
    }

    @Override
    public Boolean add(SpeciesEntity data) {
        return baseMapper.insert(data) > 0;
    }

    @Override
    public Boolean deleteByIds(Integer[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids)) == ids.length;
    }

    @Override
    public Boolean updateById(SpeciesEntity data) {
        return baseMapper.updateById(data) == 1;
    }
}
